package cn.bjtu.archiprac.papercheck.application.advice;

import cn.bjtu.archiprac.papercheck.application.constant.Constants;
import cn.bjtu.archiprac.papercheck.application.exception.IncorrectPwdException;
import cn.bjtu.archiprac.papercheck.application.exception.UserExistException;
import cn.bjtu.archiprac.papercheck.application.exception.UserNotFoundException;
import cn.bjtu.archiprac.papercheck.application.exception.UserNotLoginException;
import cn.bjtu.archiprac.papercheck.application.vo.Response;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by Zexin Lin on 4/13/2018.
 * 全局异常处理类
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    final private static Logger logger = Logger.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = UserExistException.class)
    @ResponseBody
    public Response handle(UserExistException e) {
        Response res = new Response();
        res.setStatusCode(Constants.StatusCode.BAD_REQUEST);
        res.setMsg("邮箱已注册");
        return res;
    }

    @ExceptionHandler(value = UserNotFoundException.class)
    @ResponseBody
    public Response handle(UserNotFoundException e) {
        Response res = new Response();
        res.setStatusCode(Constants.StatusCode.BAD_REQUEST);
        res.setMsg("邮箱未注册");
        return res;
    }

    @ExceptionHandler(value = IncorrectPwdException.class)
    @ResponseBody
    public Response handle(IncorrectPwdException e) {
        Response res = new Response();
        res.setStatusCode(Constants.StatusCode.BAD_REQUEST);
        res.setMsg("密码错误");
        return res;
    }

    @ExceptionHandler(value = UserNotLoginException.class)
    @ResponseBody
    public Response handle(UserNotLoginException e) {
        Response res = new Response();
        res.setStatusCode(Constants.StatusCode.UNAUTHORIZED);
        res.setMsg("用户未登录");
        return res;
    }

}
